package com.brs.model;

import java.util.Date;

public class Manager extends Person {
	private float salary;
	private boolean onDuty;

	public float getSalary() {
		return salary;
	}

	public void setSalary(float salary) {
		this.salary = salary;
	}

	public boolean isOnDuty() {
		return onDuty;
	}

	public void setOnDuty(boolean onDuty) {
		this.onDuty = onDuty;
	}

	public boolean verifyCardInfo(Card card) {
		/*
		 * 1. Put card to the card reader
		 * 2. The card reader return card info
		 * 3. Check card info
		 */
		return true;
	}

	public void issueBook(Borrower borrower, BookItem bookItem) {
		Library library = Library.getInstance();
		if (verifyCardInfo(borrower.getCard())) {
			if (borrower.getIssuedBooks().size() < borrower.getMaxIssuedBooks()) {
				Issue issue = new Issue();

				// Update status
				bookItem.setBorrowed(true);
				borrower.getIssuedBooks().add(bookItem);

				// Set issue
				issue.setBorrower(borrower);
				issue.setBookItem(bookItem);
				issue.setManager(this);
				issue.setIssuedDate(new Date());

				library.getIssues().add(issue);
			} else {
				// TODO: Borrow too many books
			}

		} else {
			// TODO: Verify card info failed
		}
	}

	public void receiveBook(BookItem bookItem) {
		Library library = Library.getInstance();
		Issue issue = null;
		for (Issue i : library.getIssues()) {
			if (i.getBookItem().getBarCode() == bookItem.getBarCode()) {
				issue = i;
				break;
			}
		}
		if (issue == null) {
			// TODO: Return a book that has not been borrowed
			return;
		}

		issue.setReturnedDate(new Date());

		// TODO: Deal with pay failure
		issue.getBorrower().payBill(issue.calBill());

		issue.getBorrower().getIssuedBooks().remove(bookItem);
		issue.getBookItem().setBorrowed(false);
	}

	@Override
	public String toString() {
		return "Manager [salary=" + salary + ", onDuty=" + onDuty + super.toString();
	}

}
